var focusStores = {};
focusStores.methods = {};
focusStores.methods.bookClass = "3";
$(function () {

    // 分类更多
    focusStores.methods.focusStoresMore();
    
    // 轮播
    //focusStores.methods.focusStoresWheel();

    // 轮播切换
    //focusStores.methods.focusStoresWheelTab();

    // 关注店铺 切换    
    focusStores.methods.focusStoresTab();

    // 多选及批量
    focusStores.methods.allLiDelete();

    // 多选及批量状态
    focusStores.methods.chooseLiImg();

    // 是否取消关注
    focusStores.methods.isCancelFocus();

    //样式状态
    focusStores.methods.allLiImg();

    // 区域显隐
    focusStores.methods.cascadeShowHide();

    // 级联
     focusStores.methods.cascade();

    // 分页
    var page = new Controller.PageControl();
    page.init({
        totalCount:100,    //总页数
        conter:".shop-pagination",    //容器选择器
        nowPage:1       //当前页，默认第几页
        // callback:fun,      
        // gotoCallBack:fun   
    });

})

// 分类更多
focusStores.methods.focusStoresMore = function () {
    if ($(".shop-user-focusStore-cut ul").height() <= $(".shop-user-focusStore-cut").height()) {
        $(".shop-user-focusStore-cut .shop-focusStore-more").hide();
    } else {
        $(".shop-user-focusStore-cut .shop-focusStore-more").show();
    }
    $(".shop-user-focusStore-cut").on("click", ".shop-focusStore-more", function () {
        if ($(this).hasClass("shop-focusStore-more-top")) {
            $(this).removeClass("shop-focusStore-more-top");
            $(this).text("更多");
            $(this).parent(".shop-user-focusStore-cut").css("height", "48px");
        } else {
            $(this).addClass("shop-focusStore-more-top");
            $(this).text("收起");
            $(this).parent(".shop-user-focusStore-cut").height($(".shop-user-focusStore-cut ul").height());
        }
    })
}

// 关注店铺 切换 
focusStores.methods.focusStoresTab = function () {
    $(".shop-user-focusStore-cut li").on("click", function () {
        focusStores.methods.allLiImg();
        $(this).addClass("shop-click").siblings().removeClass("shop-click");
        $(".shop-user-focusStore-con-classify").hide();
        focusStores.methods.bookClass  = $(this).attr("shop-book-class");
        $(".shop-user-focusStore-con-classify[shop-book-class = "+ focusStores.methods.bookClass  +"]").show();
        focusStores.methods.focusStoresWheel();
    })

}

// 轮播
focusStores.methods.focusStoresWheel = function () {
    // debugger;
    $(".shop-user-focusStore-con-classify[shop-book-class = " + focusStores.methods.bookClass + "]").find(".picScroll-left").slide({
        titcell:".hd ul",
        maincell:".bd ul",
        autopage:true,
        effect:"left",
        scroll:5,
        vis:5,
        pnloop:false
    });

}

// 轮播
//focusStores.methods.focusStoresWheel = function () {
//    $(".shop-user-focusStore-con-classify[shop-book-class=" + focusStores.methods.bookClass + "]").find(".shop-user-focusStore-con-list").each(function () {
//        var bookIndexArr = $(this).attr("shop-book-id");
//        $(".shop-user-focusStore-con-classify[shop-book-class = " + focusStores.methods.bookClass + "]").find("[shop-book-id = " + bookIndexArr + "]").find(".picScroll-left").slide({
//            titCell: ".hd ul",
//            mainCell: ".bd ul",
//            autoPage: true,
//            effect: "left",
//            scroll: 5,
//            vis: 5,
//            pnLoop: false
//        });
//    })

//}

// 轮播切换
//focusStores.methods.focusStoresWheelTab = function(){
    //$(".shop-user-focusStore-con-list-right-title h5").live("click", function () {
    //    debugger
    //    $(".shop-user-focusStore-con-list-right-con .shop-picScroll-left").hide();
    //    $(".shop-user-focusStore-con-list-right-con .shop-picScroll-left").first().show();
    //    $(this).addClass("shop-red-title").siblings().removeClass("shop-red-title");
    //    id = $(".shop-user-focusStore-con-list-right-title h5").attr("id");
    //    var datatype = $(".shop-red-title[id = " + id + "]");
    //    $(".shop-user-focusStore-con-list-right-con .shop-picScroll-left[id = " + datatype +"]").show();
    //})
//}


// 多选及批量
focusStores.methods.allLiDelete = function(){
    $(".shop-user-focusStore-completeBtn").on("click",function(){
        $(".shop-user-focusStore-allManagement").show();
        $(".shop-user-focusStore-sel-complete").hide();
        $(".shop-user-focusStore-con-list-cornerBG").hide();
        $(".shop-user-focusStore-con-list-mark").hide();
        $(".shop-user-focusStore-con-classify[shop-book-class="+ focusStores.methods.bookClass +"]").find(".shop-user-focusStore-con-list").each(function(){
            if($(this).find(".shop-user-focusStore-con-list-mark").hasClass("shop-user-focusStore-con-list-mark-red") && $(".shop-user-focusStore-cancelFocus").hasClass("shop-user-focusStore-cancelFocus-red")){
                var bookIndexArr = $(this).attr("shop-book-id");
                $(".shop-user-focusStore-con-list[shop-book-id = " + bookIndexArr + "]").remove();
            }
        })

        if($(".shop-user-focusStore-con-classify[shop-book-class="+ focusStores.methods.bookClass +"]").find(".shop-user-focusStore-con-list").length == 0){
            $(".shop-user-focusStore-sel-all input").attr("checked", false);
        }

        $(".shop-user-focusStore-cancelFocus").removeClass("shop-user-focusStore-cancelFocus-red");
    })   
}

// 多选及批量状态
focusStores.methods.chooseLiImg = function(){
    $(".shop-user-focusStore-con-list").live("click",function(){
        var bookId = $(this).attr("shop-book-id");
        if($(this).find(".shop-user-focusStore-con-list-cornerBG").css("display") == "none"){
            return;
        }else{
            $(".shop-user-focusStore-con-list[shop-book-id = " + bookId + "]").find(".shop-user-focusStore-con-list-mark").toggleClass("shop-user-focusStore-con-list-mark-red");
            var len = $(this).parents(".shop-user-focusStore-con-classify").find(".shop-user-focusStore-con-list").length;
            // 用选中的长度和书的长度判断是否全选
            if($(this).parents(".shop-user-focusStore-con-classify").find(".shop-user-focusStore-con-list-mark-red").length == len){
                $(".shop-user-focusStore-sel-all input").attr("checked", true);
            }else{
                $(".shop-user-focusStore-sel-all input").attr("checked", false);
            }
        }
    })
}

// 是否取消关注
focusStores.methods.isCancelFocus = function(){
    $(".shop-user-focusStore-cancelFocus").on("click",function(){
        $(this).toggleClass("shop-user-focusStore-cancelFocus-red");
    })
}


//样式状态
focusStores.methods.allLiImg = function () {
    // 初始状态
    $(".shop-user-focusStore-allManagement").show();
    $(".shop-user-focusStore-address").show();
    $(".shop-user-focusStore-sel-complete").hide();
    $(".shop-user-focusStore-con-list-cornerBG").hide();
    $(".shop-user-focusStore-con-list-mark").hide();
    $(".shop-user-focusStore-sel-all input").attr("checked", false);
    $(".shop-user-focusStore-con-list-mark").removeClass("shop-user-focusStore-con-list-mark-red");
    $(".shop-user-focusStore-cancelFocus").removeClass("shop-user-focusStore-cancelFocus-red");

    $(".shop-user-focusStore-allManagement").on("click", function () {
            $(".shop-user-focusStore-allManagement").hide();
            $(".shop-user-focusStore-sel-complete").show();
            $(".shop-user-focusStore-con-list-cornerBG").show();
            $(".shop-user-focusStore-con-list-mark").show();
    })

    $(".shop-user-focusStore-con-classify .shop-user-focusStore-con-list-left").live("mouseover",function(){
        if($(this).parents(".shop-user-focusStore-con-classify").find(".shop-user-focusStore-con-list-cornerBG").css("display") == "none"){
            $(this).find(".shop-focusStore-similar").show();
            $(this).find(".shop-focusStore-collection").show();
        }else{
            $(this).find(".shop-focusStore-similar").hide();
            $(this).find(".shop-focusStore-collection").hide();
        }
    }).live("mouseout",function(){
        $(this).find(".shop-focusStore-similar").hide();
        $(this).find(".shop-focusStore-collection").hide();
    })

    $(".shop-user-focusStore-sel-all input").change(function () {
        if ($(".shop-user-focusStore-sel-all input").attr("checked")) {
            $(".shop-user-focusStore-con-classify[shop-book-class="+ focusStores.methods.bookClass +"]").find(".shop-user-focusStore-con-list-mark").addClass("shop-user-focusStore-con-list-mark-red");
        } else {
            $(".shop-user-focusStore-con-classify[shop-book-class="+ focusStores.methods.bookClass +"]").find(".shop-user-focusStore-con-list-mark").removeClass("shop-user-focusStore-con-list-mark-red");
        }
    })
}







focusStores.province = null //省
focusStores.city = null //市
focusStores.area = null //区
focusStores.areas = [];
// 区域显隐
focusStores.methods.cascadeShowHide = function(){
    $(".shop-user-focusStore-address-con").on("click",".shop-focusStore-delete",function(){
        $(".shop-user-focusStore-address-con").hide();
    })

    $(".shop-user-focusStore-address").hover(function(){
        $(".shop-user-focusStore-address-con").show();
        $(".shop-user-focusStore-address-title").css("border-bottom","1px #fff solid");
    },function(){
        $(".shop-user-focusStore-address-con").hide();
        $(".shop-user-focusStore-address-title").css("border-bottom","1px #eee solid");
    })
}


focusStores.methods.cascade = function () {

    //获取城市信息
    $.get("/Metadata/GetTreeAreasAsync", {
    }, function (data) {
        focusStores.methods.levelSwitching(); //省市区选择区域切换
        $(".shop-focusStore-group").html(""); //清空头部选择区域
        focusStores.areas = data;
        focusStores.methods.AreaInitialization();
        focusStores.methods.levelSwitchingThis(0);
        $(".shop-focusStore-cascad-main").eq(0).on("click", "li", focusStores.methods.chooseProvince); // 省选择事件绑定
        $(".shop-focusStore-cascad-main").eq(1).on("click", "li", focusStores.methods.chooseCity); // 市选择事件绑定
        $(".shop-focusStore-cascad-main").eq(2).on("click", "li", focusStores.methods.chooseArea); // 区选择事件绑定
    });

}

// 省选择事件响应函数
focusStores.methods.chooseProvince = function () {
    var index = $(this).find('span').attr('data-value');
    for (var i = 0; i < focusStores.areas.length; i++) {
        if (focusStores.areas[i].id == index) {
            focusStores.province = focusStores.areas[i];
        }
    }
    focusStores.city = null;
    focusStores.area = null;
    focusStores.methods.AreaInitialization();
    focusStores.methods.levelSwitchingThis(1); 
    if(focusStores.province.children.length == 0){
        $(".shop-user-focusStore-address-title").html(focusStores.province["name"]+"<i></i>");
        $(".shop-user-focusStore-address-con").hide();
        $(".shop-focusStore-group li").eq(0).show().siblings().hide();
        $(".shop-focusStore-group li").eq(0).addClass("shop-focusStore-cascad-item-red").siblings("li").removeClass("shop-focusStore-cascad-item-red");
        $(".shop-focusStore-cascads > ol").eq(0).show().siblings().hide();
        $(".shop-user-focusStore-address-title").css("border-bottom","1px #eee solid");
    }
}
//市选择事件响应函数
focusStores.methods.chooseCity = function(){
    var index = $(this).find('span').attr('data-value');
    for (var i = 0; i < focusStores.province.children.length; i++) {
        if (focusStores.province.children[i].id == index) {
            focusStores.city = focusStores.province.children[i];
        }
    }
    focusStores.area = null;
    focusStores.methods.AreaInitialization();
    focusStores.methods.levelSwitchingThis(2);
    if(focusStores.city.children.length == 0){
        $(".shop-user-focusStore-address-title").html(focusStores.province["name"]+focusStores.city["name"]+"<i></i>");
        $(".shop-user-focusStore-address-con").hide();
        $(".shop-focusStore-group li").eq(2).hide();
        $(".shop-focusStore-group li").eq(1).addClass("shop-focusStore-cascad-item-red").siblings("li").removeClass("shop-focusStore-cascad-item-red");
        $(".shop-focusStore-cascads > ol").eq(1).show().siblings().hide();
        $(".shop-user-focusStore-address-title").css("border-bottom","1px #eee solid");
    }

}
// 区选择事件响应函数
focusStores.methods.chooseArea = function(){
    var index = $(this).find('span').attr('data-value');
    for (var i = 0; i < focusStores.city.children.length; i++) {
        if (focusStores.city.children[i].id == index) {
            focusStores.area = focusStores.city.children[i];
        }
    }
    focusStores.methods.AreaInitialization();
    focusStores.methods.levelSwitchingThis(2);
    $(".shop-user-focusStore-address-title").html(focusStores.province["name"]+focusStores.city["name"]+focusStores.area["name"]+"<i></i>");
    $(".shop-user-focusStore-address-con").hide();
    $(".shop-user-focusStore-address-title").css("border-bottom","1px #eee solid");
}


// 数据更新
focusStores.methods.AreaInitialization = function () {
    $(".shop-focusStore-group").text("");
    focusStores.methods.selectionAreaFilling(focusStores.areas, 0);
    if (focusStores.province != null) {
        focusStores.methods.dataFilling(focusStores.province);
        focusStores.methods.selectionAreaFilling(focusStores.province.children, 1)
        
    }else{
        focusStores.methods.dataFilling({text: "请选择"});
        return
    }
    if (focusStores.city != null) {
        focusStores.methods.dataFilling(focusStores.city);
        focusStores.methods.selectionAreaFilling(focusStores.city.children, 2)
    }else{
        focusStores.methods.dataFilling({text: "请选择"});
        return
    }
    if (focusStores.area != null) {
        focusStores.methods.dataFilling(focusStores.area);
    }else{
        focusStores.methods.dataFilling({text: "请选择"});
        return
    }

}

// 省市区数据填充
focusStores.methods.dataFilling = function (data) {
    $(".shop-focusStore-group").append("<li class='shop-focusStore-cascad-item'><span>" + data.text + "</span></li>")
}

// 选择区数据填充
focusStores.methods.selectionAreaFilling = function (data, index) {
    $(".shop-focusStore-cascad-main").eq(index).text("");
    for (var i = 0, len = data.length; i < len; i++) {
        $(".shop-focusStore-cascad-main").eq(index).append("<li><span data-value='" + data[i]["id"] + "'>" + data[i].name + "</span></li>")
    }
}


// 当前正在选择的[省/市/区]
focusStores.methods.levelSwitching = function(){
    $(".shop-focusStore-group").on("click", "li", function(){
        var index = $(this).index();
        focusStores.methods.levelSwitchingThis(index);
    });
}
// 指定正在选择的[省/市/区]
focusStores.methods.levelSwitchingThis = function(index){
    $(".shop-focusStore-group li").eq(index).addClass("shop-focusStore-cascad-item-red").siblings("li").removeClass("shop-focusStore-cascad-item-red");
    $(".shop-focusStore-cascads > ol").eq(index).show().siblings("ol").hide();
} 

